/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import SessionBeans.UserSessionBean;
import entities.User;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import security.Md5;

/**
 *
 * @author Tomas
 */
@ManagedBean(name = "editUserManagedBean")
@ViewScoped
public class EditUserManagedBean {
    @EJB
    private UserSessionBean userSessionBean;

    private User user;
    private String password1;
    private String password2;
    private String email;

    /**
     * Creates a new instance of EditUserManagedBean
     */
    public EditUserManagedBean() {
    }

    @PostConstruct
    public void init() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        user = (User) session.getAttribute("user");
        email = user.getEmail();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }
    
    public void editUser(){
        user.setPassword(Md5.MD5(password1));
        user.setEmail(email);
        userSessionBean.updateUser(user);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User edit", "Information edited!"));
    }
}
